package com.guofen.controller.name;

import com.guofen.pojo.Earthquake;
import com.guofen.pojo.Food;
import com.guofen.service.food.FoodService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/food")
@Api(tags = "FoodController", description = "UTA assignment")
public class FoodController {
    
    @Autowired
    private FoodService service;

    @ApiOperation("get all food")
    @RequestMapping(value = "/getAllFood",method = RequestMethod.POST)
    public Result getAllFood(@RequestParam Integer pageSize,
                             @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getAllFood(pageSize, currentPage));
    }

    @ApiOperation("get food by cost")
    @RequestMapping(value = "/getByCost",method = RequestMethod.POST)
    public Result getByCost(@RequestParam Integer cost1,
                           @RequestParam Integer cost2,
                           @RequestParam Integer pageSize,
                           @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByCost(cost1, cost2, pageSize, currentPage));
    }

    @ApiOperation("get food by cost and type")
    @RequestMapping(value = "/getByCostType",method = RequestMethod.POST)
    public Result getByCostType(@RequestParam Integer cost1,
                                @RequestParam Integer cost2,
                                @RequestParam String type,
                                @RequestParam Integer pageSize,
                                @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByCostType(cost1, cost2, type, pageSize, currentPage));
    }

    @ApiOperation("get food by cost and color")
    @RequestMapping(value = "/getByCostColor",method = RequestMethod.POST)
    public Result getByCostColor(@RequestParam Integer cost1,
                                 @RequestParam Integer cost2,
                                 @RequestParam String color,
                                 @RequestParam Integer pageSize,
                                 @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByCostColor(cost1, cost2, color, pageSize, currentPage));
    }

    @ApiOperation("get food by type")
    @RequestMapping(value = "/getByType",method = RequestMethod.POST)
    public Result getByType(@RequestParam String type,
                            @RequestParam Integer pageSize,
                            @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByType(type, pageSize, currentPage));
    }

    @ApiOperation("get food by types")
    @RequestMapping(value = "/getByTypes",method = RequestMethod.POST)
    public Result getByTypes(@RequestParam String[] types,
                             @RequestParam(defaultValue = "8") Integer pageSize,
                             @RequestParam(defaultValue = "1") Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByTypes(types, currentPage, pageSize));
    }

    @ApiOperation("get distinct type names")
    @RequestMapping(value = "/getDistinctTypes",method = RequestMethod.POST)
    public Result getDistinctTypes() {
        return ResultGenerator.genSuccessResult(service.getDistinctTypes());
    }

    @ApiOperation("get foods group by type")
    @RequestMapping(value = "/getGroupByType",method = RequestMethod.POST)
    public Result getGroupByType() {
        return ResultGenerator.genSuccessResult(service.getGroupByType());
    }

    @ApiOperation("get name by type")
    @RequestMapping(value = "/getNameByType",method = RequestMethod.POST)
    public Result getNameByType(@RequestParam String type) {
        return ResultGenerator.genSuccessResult(service.getNameByType(type));
    }

    @ApiOperation("get food by color")
    @RequestMapping(value = "/getByColor",method = RequestMethod.POST)
    public Result getByColor(@RequestParam String color,
                             @RequestParam Integer pageSize,
                             @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByColor(color, pageSize, currentPage));
    }

    @ApiOperation("get food by type and color")
    @RequestMapping(value = "/getByTypeColor",method = RequestMethod.POST)
    public Result getByTypeColor(@RequestParam String type,
                                  @RequestParam String color,
                                  @RequestParam Integer pageSize,
                                  @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(service.getByTypeColor(type, color, pageSize, currentPage));
    }

    @ApiOperation("get type distribution")
    @RequestMapping(value = "/getTypeDistribution",method = RequestMethod.GET)
    public Result getTypeDistribution() {
        return ResultGenerator.genSuccessResult(service.getTypeDistribution());
    }

    @ApiOperation("get color distribution")
    @RequestMapping(value = "/getColorDistribution",method = RequestMethod.GET)
    public Result getColorDistribution() {
        return ResultGenerator.genSuccessResult(service.getColorDistribution());
    }

    @ApiOperation("get color distribution selective")
    @RequestMapping(value = "/getColorDisSelective",method = RequestMethod.POST)
    public Result getColorDisSelective(@RequestParam String labels) {
        return ResultGenerator.genSuccessResult(service.getColorDisSelective(labels));
    }

    @ApiOperation("get cost distribution")
    @RequestMapping(value = "/getCostDistribution",method = RequestMethod.POST)
    public Result getCostDistribution(@RequestParam Integer groupNum) {
        return ResultGenerator.genSuccessResult(service.getCostDistribution(groupNum));
    }

    @ApiOperation("insert")
    @PostMapping("/insert")
    public Result insert(@RequestParam String name, @RequestParam String type,
                         @RequestParam Integer cost, @RequestParam Integer stock) {
        Food food = new Food();
        food.setName(name);
        food.setType(type);
        food.setCost(cost);
        food.setStock(stock);
        return ResultGenerator.genSuccessResult(service.insert(food));
    }

    @ApiOperation("delete")
    @PostMapping("/delete")
    public Result delete(@RequestParam("name") String name) {
        return ResultGenerator.genSuccessResult(service.delete(name));
    }

    @ApiOperation("update")
    @PostMapping("/update")
    public Result upload(@RequestParam String name,
                         @RequestParam String type,
                         @RequestParam Integer cost,
                         @RequestParam Integer stock){
        Food food = service.getByName(name);
        food.setType(type);
        food.setCost(cost);
        food.setStock(stock);
        return ResultGenerator.genSuccessResult(service.update(food));
    }
}
